package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.entity.UserOpLog;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.MemberType;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.business.UserInfo;
import com.tbit.uqbike.object.pojo.MemberFee;
import com.tbit.uqbike.object.pojo.UserMember;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.MemberFeeService;
import com.tbit.uqbike.webmanager.business.user_op_log.service.UserOpLogService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 免押会员费
 *
 * @author Leon
 * 2019年9月23日 下午7:08:18
 */
@Validated
@RestController
@RequestMapping(value = "/memberFee", produces = BaseController.MEDIA_JSON_UTF8)
public class MemberFeeController extends BaseController {
    @Autowired
    private MemberFeeService memberFeeService;
    @Autowired
    private UserOpLogService userOpLogService;

    /* 添加 */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.DEPOSITCARD, methods = MethodEnum.MEMBERFEE_ADD)
    public JsonResult add(MemberFee memberFee, HttpServletRequest request) {
        /* 校验 */
        Assert.notNull(memberFee.getType(), "ver.type.notNull");
        Assert.notNull(memberFee.getName(), "ver.name.notNull");
        Assert.notNull(memberFee.getMoney(), "ver.money.notNull");
        LoginInfo info = getLogin(request);
        if (!info.type.equals(UserConstant.USER_AGENT)) {
            Assert.notNull(memberFee.getAccountId(), "ver.accountId.notNull");
        } else {
            memberFee.setAccountId(info.id);
        }
        if (!MemberType.Check(memberFee.getType().toString())) {
            throw new BaseException(SystemCodeEnum.UNKNOWN_TYPE);
        }

        if (memberFee.getMoney() <= 0) {
            throw new BaseException(SystemCodeEnum.AMMOUNT_MUST_GREATER_THAN_0);
        }
        if (memberFee.getName().length() > 100){
            throw new BaseException("免押会员名称过长");
        }
        memberFeeService.add(memberFee);

        // 插入用户操作日志
        userOpLogService.insertSingle(UserOpLog.builder()
                .accountId(memberFee.getAccountId())
                .opUser(info.getName())
                .opUserPhone(info.getPhone())
                .operation("免押卡")
                .opType("创建")
                .opPlatform(OperationPlatform.MANAGER.code)
                .opTime(LocalDateTime.now())
                .result(true)
                .build());

        return JsonResult.ok();
    }

    /* 更新 */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.DEPOSITCARD, methods = MethodEnum.MEMBERFEE_EDIT)
    public JsonResult update(MemberFee memberFee, HttpServletRequest request) {
        /* 校验 */
        Assert.notNull(memberFee.getMemberFeeId(), "ver.menberFeeId.notNull");
        LoginInfo info = getLogin(request);
        if (!info.type.equals(UserConstant.USER_AGENT)) {
            Assert.notNull(memberFee.getAccountId(), "ver.accountId.notNull");
        } else {
            memberFee.setAccountId(info.id);
        }
        if (memberFee.getType() != null) {
            if (!MemberType.Check(memberFee.getType().toString())) {
                throw new BaseException(SystemCodeEnum.UNKNOWN_TYPE);
            }
        }
        if (memberFee.getMoney() != null && memberFee.getMoney() <= 0) {
            throw new BaseException(SystemCodeEnum.AMMOUNT_MUST_GREATER_THAN_0);
        }

        if (memberFee.getRemark() == null) {
            memberFee.setRemark("");
        }
        if (memberFee.getDescription() == null) {
            memberFee.setDescription("");
        }
        if (StrUtil.isNotEmpty(memberFee.getName()) && memberFee.getName().length() > 100){
            throw new BaseException("免押会员名称过长");
        }
        memberFeeService.update(memberFee);

        return JsonResult.ok();
    }

    /* 查找 */
    @RequestMapping("/queryPage")
    public JsonResult find(Integer accountId,
                           @RequestParam(defaultValue = "10") Integer rowCount,
                           @RequestParam(defaultValue = "1") Integer pageNO,
                           HttpServletRequest request) {
        LoginInfo info = getLogin(request);
        if (!info.type.equals(UserConstant.USER_AGENT)) {
            Assert.notNull(accountId, "ver.accountId.notNull");
        } else {
            accountId = info.id;
        }
        /*获取总数*/
        Integer total = memberFeeService.queryCount(accountId);
        List<MemberFee> memberFees = memberFeeService.find(accountId, rowCount, pageNO);

        return JsonResult.ok(memberFees, total);
    }

    /**
     * 置为失效
     *
     * @param memberFeeId
     * @param request
     * @param accountId
     * @return
     */
    @RequestMapping("/edit")
    @OperateLog(module = ModuleEnum.DEPOSITCARD, methods = MethodEnum.MEMBERFEE_CANCAL)
    public JsonResult edit(Integer memberFeeId, HttpServletRequest request, Integer accountId) {
        LoginInfo info = getLogin(request);
        if (!info.type.equals(UserConstant.USER_AGENT)) {
            Assert.notNull(accountId, "ver.accountId.notNull");
        } else {
            accountId = info.id;
        }
        memberFeeService.edit(memberFeeId, accountId);
        return JsonResult.ok();
    }

    /**
     * 用户购买的免押套餐记录分页
     *
     * @param userId
     * @param rowCount
     * @param pageNO
     * @return
     */
    @RequestMapping("/userMemPage")
    public JsonResult userMemPage(Integer userId,
                                  @RequestParam(defaultValue = "10") Integer rowCount,
                                  @RequestParam(defaultValue = "1") Integer pageNO) {
        Assert.notNull(userId, "ver.userId.notNull");
        /*获取总数*/
        Integer total = memberFeeService.userMemCount(userId);
        List<UserMember> userMembers = memberFeeService.userMemPage(userId, rowCount, pageNO);

        return JsonResult.ok(userMembers, total);
    }
}
